using System;

namespace PrismNavigationFramework.Navigation
{
    public class ViewNavigationContextInformationParsingService : IViewNavigationContextInformationParsingService
    {
        private readonly IPrismNavigationConfiguration _prismNavigationConfiguration;

        public ViewNavigationContextInformationParsingService(IPrismNavigationConfiguration prismNavigationConfiguration)
        {
            _prismNavigationConfiguration = prismNavigationConfiguration;
        }

        public ViewNavigationContextInformation ParsePath(string path)
        {
            var result = new ViewNavigationContextInformation();

            if (string.IsNullOrEmpty(path))
            {
                return result;
            }

            var parts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            result.ModuleName = parts.Length > 1 ? parts[0] : this._prismNavigationConfiguration.DefaultModuleName;

            result.ViewName = parts.Length > 0
                                  ? (parts.Length > 1 ? parts[1] : parts[0])
                                  : this._prismNavigationConfiguration.DefaultViewName;

            if (string.IsNullOrEmpty(result.ModuleName))
            {
                result.ModuleName = this._prismNavigationConfiguration.DefaultModuleName;
            }

            if (string.IsNullOrEmpty(result.ViewName))
            {
                result.ViewName = this._prismNavigationConfiguration.DefaultViewName;
            }

            return result;
        }

    }
}